package com.mathdroid.gui.main.character;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JToolTip;

import com.mathdroid.gui.main.ItemToolTip;
import com.mathdroid.model.Model;
import com.mathdroid.model.enums.EItemSlot;
import com.mathdroid.model.enums.EItemType;
import com.mathdroid.model.enums.EModuleProficiency;
import com.mathdroid.model.items.Item;

/**
 * This button overrides some of the JButton ToolTip methods, so that a custom
 * ToolTip can be used.
 * @see ItemToolTip
 */
public final class ItemButton extends JButton {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The item tooltip.
	 */
	private ItemToolTip tip = null;
	
	/**
	 * The horizontal offset of the tooltip.
	 */
	private static final int TOOL_TIP_HORIZONTAL_OFFSET = 20;
	
	/**
	 * The slot of the item.
	 */
	private EItemSlot slot = null;
	
	/**
	 * The type of the source slot.
	 */
	private EItemType type = null;
	
	/**
	 * The type of module in the slot, if this is a module.
	 */
	private EModuleProficiency module = null;
	
	/**
	 * Create a JButton with ItemToolTip.
	 * @see ItemToolTip
	 * @param s The item slot type.
	 * @param m The module type.
	 * @param t The type of the item in this slot.
	 */
	public ItemButton(
			final EItemSlot s, final EModuleProficiency m, final EItemType t) {
		super();
		
		this.slot = s;
		this.module = m;
		this.type = t;
		
		this.setToolTipText("");
		
		this.buildMouseListener();
	}

	/**
	 * Add the mouse listener to this button.
	 */
	private void buildMouseListener() {

		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(final MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					new ItemSlotContextMenu(slot, module, e);
				} else {
					new ItemSlotPopup(slot, module, type);
				}
			}
		});
	}
	
	@Override
	public JToolTip createToolTip() {
		if (this.tip == null) {
			this.tip = new ItemToolTip();
			this.tip.setComponent(this);
		}
		return this.tip;
	}

	@Override
	public String getToolTipText(final MouseEvent e) {
		String s = "" + this.slot;
		
		if (this.tip != null && this.module == null) {
			/* "Primary" Slot */
			this.tip.setItem(Model.getProfile().getEquipedItems()
					.get(this.slot));
		} else if (this.tip != null && this.module != null) {
			/* Module Slot */
			final Item i = Model.getProfile().getEquipedItems()
					.get(this.slot);
			if (i != null) {
				if (i.getName() != null) {
					this.tip.setItem(i.getModules().get(this.module));
				}
			}
		}
		
		if (this.module != null) {
			s =  s + ": " + this.module;
		}
		
		return s;
	}
	
	@Override
	public Point getToolTipLocation(final MouseEvent e) {
		Point p = e.getPoint();
		p.x += TOOL_TIP_HORIZONTAL_OFFSET;
		return p;
	 }
	
	/**
	 * If this button is an item module.
	 * @return This is an item module.
	 */
	public boolean isModule() {
		boolean b = true;
		
		if (this.module == null) {
			b = false;
		}
		
		return b;
	}
}
